﻿<cfscript>
/** 
* @hint "system - user authorized group operation"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.system.userGroupListener" 
	output=false 
	depends="datetimeAdvice, sequenceUserGroup" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	

	/**
	* @hint "创建一个用户组授权"
	* 
	* @event "MachII Event Object"
	*/
	public void function createUserGroup ( required MachII.framework.Event event ) output=false {
		
		var userId = event.getArg("uid");
		var groupId = event.getArg("gid");

		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 检查用户 */
			sql = "SELECT 1 FROM t_user WHERE user_id = :userId ";

			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="userId", value=userId, cfsqltype="cf_sql_varchar" );

			var rs_user = queryObj.execute( sql=sql ).getResult();
			
			/* 检查角色 */
			sql = "SELECT 1 FROM t_group WHERE group_id = :groupId ";

			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="groupId", value=groupId, cfsqltype="cf_sql_varchar" );

			var rs_group = queryObj.execute( sql=sql ).getResult();
			
			if ( rs_user.recordCount eq 1 and rs_group.recordCount eq 1 ) {
				
				/* 当前日期时间字符串 */
				var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
				
				/* 生成主键 */
				var sequence = getsequenceUserGroup();

				sequence.setUID( userId );
				sequence.setGID( groupId );

				var userGroupId = sequence.getID();

				/* 检查用户组授权 */
				sql = "SELECT 1 FROM t_groupuser WHERE ugid = :userGroupId ";

				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="userGroupId", value=userGroupId, cfsqltype="cf_sql_varchar" );

				var rs_check = queryObj.execute( sql=sql ).getResult();

				if ( rs_check.recordCount eq 0 ) {

					sql = "INSERT INTO t_groupuser ( ugid, user_id, group_id, date_create, date_lastupdate ) VALUES ( :userGroupId, :userId, :groupId, :currentDateTime, :currentDateTime )";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="userGroupId", value=userGroupId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="userId", value=userId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="groupId", value=groupId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );

				}
				else {
					
					sql = "UPDATE t_groupuser SET date_lastupdate = :currentDateTime WHERE ugid = :userGroupId ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="userGroupId", value=userGroupId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );
					
				}
				
			}
			
		}
	}
	
	/**
	* @hint "删除一个用户组授权"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeUserGroup ( required MachII.framework.Event event ) output=false {
		
		var userId = event.getArg("uid");
		var groupId = event.getArg("gid");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 生成主键 */
			var sequence = getsequenceUserGroup();

			sequence.setUID( userId );
			sequence.setGID( groupId );

			var userGroupId = sequence.getID();
				
			/* 检查用户组授权 */
			sql = "SELECT 1 FROM t_groupuser WHERE ugid = :userGroupId ";

			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="userGroupId", value=userGroupId, cfsqltype="cf_sql_varchar" );

			var rs_check = queryObj.execute( sql=sql ).getResult();

			if ( rs_check.recordCount eq 1 ) {
				
				sql = "DELETE FROM t_groupuser WHERE ugid = :userGroupId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="userGroupId", value=userGroupId, cfsqltype="cf_sql_varchar" );

				queryObj.execute( sql=sql );
				
			}

		}
	}

	
}

</cfscript>